package com.example.ibo.lollipopdemo.Frag;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.ibo.lollipopdemo.Adapters.MyPagerAdapter;
import com.example.ibo.lollipopdemo.Informatoin;
import com.example.ibo.lollipopdemo.R;
import com.example.ibo.lollipopdemo.VivzAdapter;

import java.util.ArrayList;
import java.util.List;

public class MyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, VivzAdapter.ClickListener {
    private static MyPagerAdapter adapter;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;

    public static MyFragment getInstance(MyPagerAdapter adapter, int position) {
        MyFragment.adapter = adapter;
        MyFragment myFragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        myFragment.setArguments(args);
        return myFragment;
    }

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_my, container, false);

        recyclerView = (RecyclerView) layout.findViewById(R.id.recycle_view);
        swipeRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.swipe_refresh);

        Bundle bundle = getArguments();
        if (bundle != null) {
            int position = bundle.getInt("position");
            if (adapter.getPageTitle(position).toString().contains("Tab")) {
                swipeRefreshLayout.setVisibility(View.GONE);
            } else {
                swipeRefreshLayout.setOnRefreshListener(this);
                showAdapter(position);
            }
        }
        return layout;
    }

    private void showAdapter(int position) {
        List<Informatoin> data = new ArrayList<>();

        if (position == 0) {
            data.add(new Informatoin(R.mipmap.ic_launcher, "全部订单 1"));
            data.add(new Informatoin(R.mipmap.ic_launcher, "全部订单 2"));
            data.add(new Informatoin(R.mipmap.ic_launcher, "全部订单 3"));
            data.add(new Informatoin(R.mipmap.ic_launcher, "全部订单 4"));
        } else if (position == 1) {
            data.add(new Informatoin(R.mipmap.ic_launcher, "待付款订单 1"));
            data.add(new Informatoin(R.mipmap.ic_launcher, "待付款订单 2"));
            data.add(new Informatoin(R.mipmap.ic_launcher, "待付款订单 3"));
            data.add(new Informatoin(R.mipmap.ic_launcher, "待付款订单 4"));
        }

        VivzAdapter TempAdapter = new VivzAdapter(getContext(), data);
        TempAdapter.setOnIteamClickListener(this);
        recyclerView.setAdapter(TempAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                VivzAdapter ad = ((VivzAdapter) recyclerView.getAdapter());
                if (ad.getItemCount() > 0) {
                    ad.delete(0);
                }
                if (swipeRefreshLayout.isRefreshing())
                    swipeRefreshLayout.setRefreshing(false);
            }
        }, 1500);
    }

    @Override
    public void itemClicked(View view, int position) {
        VivzAdapter ad = ((VivzAdapter) recyclerView.getAdapter());
        Snackbar.make(view,ad.getItem(position), Snackbar.LENGTH_SHORT).show();
    }
}